//
//  TDImageViewController.m
//  TotalDemo
//
//  Created by gujian on 16/6/24.
//  Copyright © 2016年 gujian. All rights reserved.
//

#import "TDImageViewController.h"
#import "UIImage+Extension.h"

@implementation TDImageViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"处理图像";
    [self initSubViews];
}

- (void)initSubViews
{
    CGFloat imgWidth = 70;
    CGFloat imgHeight = 70;
    
    UIImageView *originImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, imgWidth, imgHeight)];
    originImgView.image = [UIImage imageNamed:@"cat.jpg"];
    [self.view addSubview:originImgView];
    
    UIImage *originImage = [UIImage imageNamed:@"cat.jpg"];

    
    NSArray *orientationArr = @[@(UIImageOrientationUp),@(UIImageOrientationUpMirrored),
                                @(UIImageOrientationDown),@(UIImageOrientationDownMirrored),
                                @(UIImageOrientationLeft),@(UIImageOrientationLeftMirrored),
                                @(UIImageOrientationRight),@(UIImageOrientationRightMirrored)];
    
    CGFloat offX = 100;
    CGFloat offY = originImgView.bottom + 10;
    
    
    for (int i = 0; i < orientationArr.count; i++) {
        UIImage *targetImage = [originImage rotateImageWithorientation:[orientationArr[i] integerValue]];
        UIImageView *downImageView = [[UIImageView alloc] initWithFrame:CGRectMake(offX, offY, imgWidth, imgHeight)];
        downImageView.image = targetImage;
        downImageView.backgroundColor = [UIColor randomColor];
        [self.view addSubview:downImageView];
        
        if ((i+1) % 2 == 0) {
            offX = 100;
            offY += (imgHeight +10);
        }else{
            offX += (downImageView.width + 50);
           
        }
        
        
    }
    
}


/** 翻转图片 */

- (UIImage *)rotateImage:(UIImage *)originImage orientation:(UIImageOrientation)orientation
{
    CGSize targetImgSize = originImage.size;
    CGRect targetRect = CGRectMake(0, 0, targetImgSize.width, targetImgSize.height);

    
    if (originImage.imageOrientation != UIImageOrientationUp) {
        LotLog(@"原始图像方向：%@",@(originImage.imageOrientation));
        return originImage;
    }
    
    CGImageRef imgRef = originImage.CGImage;
    CGAffineTransform transform = CGAffineTransformIdentity;
    
    switch (orientation) {
        case UIImageOrientationUp:
            transform = CGAffineTransformIdentity;
            break;
        case UIImageOrientationDown:
            transform = CGAffineTransformRotate(transform, M_PI);
            break;
        case UIImageOrientationLeft:
            transform = CGAffineTransformRotate(transform, M_PI_2);
            break;
        case UIImageOrientationRight:
            break;
        case UIImageOrientationUpMirrored:
            break;
        case UIImageOrientationDownMirrored:
            break;
        case UIImageOrientationLeftMirrored:
            break;
        case UIImageOrientationRightMirrored:
            break;

    }
    
    UIGraphicsBeginImageContextWithOptions(targetImgSize, NO, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextConcatCTM(context, transform);
//    CGContextScaleCTM(context, 1.0, -1.0);
    CGContextDrawImage(context, targetRect, imgRef);
    
    UIImage *targetImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return targetImage;
}


/** 截屏 */
- (UIImage *)screenShot
{
    CGSize imgSize = [[UIScreen mainScreen] bounds].size;
    
    if (NULL != &UIGraphicsBeginImageContextWithOptions) {
        UIGraphicsBeginImageContextWithOptions(imgSize, NO, 0);
    }
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunreachable-code"
    else
    {
       UIGraphicsBeginImageContext(imgSize);
    }
#pragma clang diagnostic pop
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    for (UIWindow *window in [[UIApplication sharedApplication] windows]) {
        if (![window respondsToSelector:@selector(screen)] || [window screen] == [UIScreen mainScreen]) {
            
            [[window layer] renderInContext:context];
            
        }
    }
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    CGContextRelease(context);
    
    return image;
}

@end
